import com.liao.utils.HbaseUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class HBaseScanUtil {

    private List<String[]> list;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    private int i=0;

    public List<String[]> init(String phone,String start,String stop) throws ParseException {
        list = new ArrayList<>();
//      将日期转为date类型作为日历类型的参数  Fri Jul 16 14:45:24 CST 2021
        Date startDate = sdf.parse(start);
        System.out.println("startDate:"+startDate);
        Date stopDate = sdf.parse(stop);
        System.out.println("stopDate:"+stopDate);

//      将Date类型转为日历类型 方便相加
        Calendar startPoint = Calendar.getInstance();
         startPoint.setTime(startDate);
        Calendar stopPoint = Calendar.getInstance();
        stopPoint.setTime(startDate);
        stopPoint.add(Calendar.MONTH,1);

        //      当前接收时间的时间戳和总的结束时间的时间戳进行对比
        while (stopPoint.getTimeInMillis() <= stopDate.getTime()){
//          getTime方法拿到Date类型的日期
            String startTime = sdf.format(startPoint.getTime());
            String stopTime = sdf.format(stopPoint.getTime());
//            开始时间的分区号  要和 当前结束时间的分区号一致
            String regionHash = HbaseUtil.getRegionHash(phone, startTime, 6);
            String startRow = regionHash+"_"+phone+"_"+startTime;
            String stopRow = regionHash+"_"+phone+"_"+stopTime;

            String[] rowKeys = {startRow,stopRow};
            list.add(rowKeys);

            startPoint.add(Calendar.MONTH,1);
            stopPoint.add(Calendar.MONTH,1);

        }

        return list;
    }
    public boolean hasNext(){
        return i<list.size();
    }
    public  String[] next(){
        return list.get(i++);
    }
}
